home *** CD-ROM | disk | FTP | other *** search
Wrap
Java Source | 2001-06-23 | 13.1 KB | 463 lines
/* Polyhedra by Scott Ziegler 06.22.01 For use with Simulcra RPG and AD&D. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications. Any use of this software for any purpose whatsoever must have explicit permission from the author of the file. This code is part of an ongoing development process for future possible products, and so is considered private property. Do not use without permission. Author: Scott C. Ziegler <Aslan@Narnia.net> Instructions for use: Code in use by: version history: 10.04.00 - Began the project. 10.05.00 - reached a syntactically correct version. 06.22.01 - refitted project with Project Builder (MacOS X) and renamed to Polyhedra from DiceRoller at MacHack 2001 (16th/5th) Work Needed: Work Completed: */ //package Polyhedra; import java.awt.*; import java.awt.event.*; import java.awt.Window; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class Polyhedra extends JFrame { public static final Font DEFAULT_SYS_FONT = new Font("Courier", Font.PLAIN, 10); public static final Font DEFAULT_ROLL_FONT = new Font("Courier", Font.PLAIN, 36); public static final String DEFAULT_SYS_PROMPT = new String(">"); public static final Arcana.ADDDice DEFAULT_DIE = new Arcana.ADDDice(); JMenuBar menuBar; JMenu fileMenu; JMenuItem aboutBoxMenuItem; JMenuItem quitMenuItem; JMenu editMenu; JMenuItem undoMenuItem; JMenuItem cutMenuItem; JMenuItem copyMenuItem; JMenuItem pasteMenuItem; JMenu optionsMenu; JMenuItem optionsMenuItem; JMenu rollMenu; JMenuItem rollMenuItem; JButton rollBtn; JButton twoSidedBtn; JButton threeSidedBtn; JButton fourSidedBtn; JButton sixSidedBtn; JButton eightSidedBtn; JButton tenSidedBtn; JButton twelveSidedBtn; JButton twentySidedBtn; JButton thirtySidedBtn; JButton hundredSidedBtn; JCheckBox timesCheckBox; JLabel rollLabel; JTextArea displayTextArea; JTextField dieTextField; JTextField timesTextField; JScrollPane displayScrollPane; JScrollBar displayScrollBar; BoundedRangeModel scrollModel; int oldMax; JPanel devicePanel; JPanel buttonPanel; JPanel checkPanel; Font systemFont; Font rollFont; String systemPrompt; boolean systemPromptSwitch; ActionListener diceButtonListener; Arcana.ADDDice mainDie; public Polyhedra() { super("Polyhedra"); addWindowListener(new BasicWindowMonitor()); Insets objectInsets = new Insets(2,2,2,2); mainDie = DEFAULT_DIE; setSystemPromptSwitch(false); setSystemPrompt(DEFAULT_SYS_PROMPT); systemFont = DEFAULT_SYS_FONT; rollFont = DEFAULT_ROLL_FONT; // Menus and MenuBar and MenuItems menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); aboutBoxMenuItem = new JMenuItem("About…"); fileMenu.add(aboutBoxMenuItem); aboutBoxMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { /*printEndLine("\nThoth Interaction System \n" + "Written by Scott Ziegler \n" + "¢ Simulcra 1999"); */ } }); quitMenuItem = new JMenuItem("Quit"); fileMenu.add(quitMenuItem); quitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); editMenu = new JMenu("Edit"); undoMenuItem = new JMenuItem("Undo"); editMenu.add(undoMenuItem); cutMenuItem = new JMenuItem("Cut"); editMenu.add(cutMenuItem); copyMenuItem = new JMenuItem("Copy"); editMenu.add(copyMenuItem); pasteMenuItem = new JMenuItem("Paste"); editMenu.add(pasteMenuItem); optionsMenu = new JMenu("Options"); optionsMenuItem = new JMenuItem("Options…"); optionsMenu.add(optionsMenuItem); rollMenu = new JMenu("Roll"); rollMenuItem = new JMenuItem("Roll…"); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(optionsMenu); menuBar.add(rollMenu); // Roll Button rollBtn = new JButton("Roll"); rollBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { // setRollLabel(/*call to aux. which returns String of roll*/); if (dieTextField.getText() != "") { mainDie = mainDie.stringToDie(dieTextField.getText()); if (!timesCheckBox.getModel().isSelected()) { int roll = mainDie.roll(); setRollLabel("" + roll); displayPrompt(); displayPrintLine("" + roll); } else { try { int numberOfRolls = Integer.parseInt(timesTextField.getText()); displayPrint("" + numberOfRolls +"x,"); displayPrompt(); int rollHolder; for (int i=0; i < numberOfRolls; i++) { rollHolder = mainDie.roll(); displayPrint("" + rollHolder + ","); } displayPrint("."); displayEndline(); } catch (NumberFormatException nfe) { } } } } }); // Dice Buttons (all in one handler) diceButtonListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { String com = ae.getActionCommand(); if (com.compareTo("1d2") == 0) { mainDie.setCoefficient(1); mainDie.setDie(2); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d3") == 0) { mainDie.setCoefficient(1); mainDie.setDie(3); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d4") == 0) { mainDie.setCoefficient(1); mainDie.setDie(4); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d6") == 0) { mainDie.setCoefficient(1); mainDie.setDie(6); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d8") == 0) { mainDie.setCoefficient(1); mainDie.setDie(8); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d10") == 0) { mainDie.setCoefficient(1); mainDie.setDie(10); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d12") == 0) { mainDie.setCoefficient(1); mainDie.setDie(12); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d20") == 0) { mainDie.setCoefficient(1); mainDie.setDie(20); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d30") == 0) { mainDie.setCoefficient(1); mainDie.setDie(30); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } else if (com.compareTo("1d100") == 0) { mainDie.setCoefficient(1); mainDie.setDie(100); mainDie.setModifier(0); setRollLabel("" + mainDie.roll()); } } }; // Dice Button Initialization twoSidedBtn = new JButton("1d2"); twoSidedBtn.addActionListener(diceButtonListener); threeSidedBtn = new JButton("1d3"); threeSidedBtn.addActionListener(diceButtonListener); fourSidedBtn = new JButton("1d4"); fourSidedBtn.addActionListener(diceButtonListener); sixSidedBtn = new JButton("1d6"); sixSidedBtn.addActionListener(diceButtonListener); eightSidedBtn = new JButton("1d8"); eightSidedBtn.addActionListener(diceButtonListener); tenSidedBtn = new JButton("1d10"); tenSidedBtn.addActionListener(diceButtonListener); twelveSidedBtn = new JButton("1d12"); twelveSidedBtn.addActionListener(diceButtonListener); twentySidedBtn = new JButton("1d20"); twentySidedBtn.addActionListener(diceButtonListener); thirtySidedBtn = new JButton("1d30"); thirtySidedBtn.addActionListener(diceButtonListener); hundredSidedBtn = new JButton("1d100"); hundredSidedBtn.addActionListener(diceButtonListener); // JLabel decl and event handler for redraw rollLabel = new JLabel("0000"); rollLabel.setFont(rollFont); rollLabel.setHorizontalAlignment(SwingConstants.CENTER); rollLabel.setVerticalAlignment(SwingConstants.CENTER); // JTextArea displayTextArea = new JTextArea(/*20,30*/); // this size doesn't work. displayTextArea.setEditable(false); displayTextArea.setLineWrap(true); displayTextArea.setWrapStyleWord(true); displayTextArea.setFont(systemFont); // JScrollPane and JScrollBar "advancer" displayScrollPane = new JScrollPane(displayTextArea); displayScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); displayScrollBar = displayScrollPane.getVerticalScrollBar(); scrollModel = displayScrollBar.getModel(); oldMax = 0; scrollModel.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { BoundedRangeModel sourceModel = (BoundedRangeModel)ce.getSource(); int newMax = sourceModel.getMaximum(); if (newMax > oldMax) { scrollModel.setValue(newMax); oldMax = newMax; } } }); // JTextFields dieTextField = new JTextField(); dieTextField.setFont(systemFont); timesTextField = new JTextField(); timesTextField.setFont(systemFont); // JCheckBox decl and event handler (grey-out when unchecked) timesCheckBox = new JCheckBox("Times:", false); timesCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if (ie.getStateChange() == ItemEvent.SELECTED) { timesTextField.setEnabled(true); } if (ie.getStateChange() == ItemEvent.DESELECTED) { timesTextField.setEnabled(false); } } }); // Set up Layout TitledBorder dieTitle = new TitledBorder("Die"); dieTitle.setTitleJustification(TitledBorder.LEFT); dieTitle.setTitlePosition(TitledBorder.TOP); dieTextField.setBorder(dieTitle); TitledBorder rollTitle = new TitledBorder("Roll"); rollTitle.setTitleJustification(TitledBorder.LEFT); rollTitle.setTitlePosition(TitledBorder.TOP); rollLabel.setBorder(rollTitle); checkPanel = new JPanel(new GridLayout(2,1)); checkPanel.add(timesCheckBox); checkPanel.add(timesTextField); devicePanel = new JPanel(); devicePanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = 80; c.weighty = 25; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; c.insets = objectInsets; devicePanel.add(dieTextField, c); c.gridx = 1; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = 20; c.weighty = 25; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; c.insets = objectInsets; devicePanel.add(rollBtn, c); c.gridx = 0; c.gridy = 1; c.gridheight = 1; c.gridwidth = 1; c.weightx = 80; c.weighty = 75; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; c.insets = objectInsets; devicePanel.add(rollLabel, c); c.gridx = 1; c.gridy = 1; c.gridheight = 1; c.gridwidth = 1; c.weightx = 20; c.weighty = 75; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.NORTH; c.insets = objectInsets; devicePanel.add(checkPanel, c); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2,5,5,5)); // 2 rows, 5 cols, 5 hpix, 5 vpix // should I set insets? buttonPanel.add(twoSidedBtn); buttonPanel.add(threeSidedBtn); buttonPanel.add(fourSidedBtn); buttonPanel.add(sixSidedBtn); buttonPanel.add(eightSidedBtn); buttonPanel.add(tenSidedBtn); buttonPanel.add(twelveSidedBtn); buttonPanel.add(twentySidedBtn); buttonPanel.add(thirtySidedBtn); buttonPanel.add(hundredSidedBtn); // initialize window, add components, and show setJMenuBar(menuBar); Container progFrameContentPane = getContentPane(); progFrameContentPane.add(devicePanel, BorderLayout.NORTH); progFrameContentPane.add(displayScrollPane, BorderLayout.CENTER); progFrameContentPane.add(buttonPanel, BorderLayout.SOUTH); setSize(new Dimension(385,440)); setVisible(true); dieTextField.requestFocus(); } // Selectors public String getSystemPrompt() { return systemPrompt; } public boolean getSystemPromptSwitch() { return systemPromptSwitch; } public Font getSystemFont() { return systemFont; } public Font getRollFont() { return rollFont; } // Mutators public void setSystemPrompt(String prompt) { systemPrompt = prompt; } public void setSystemPromptSwitch(boolean bool) { systemPromptSwitch = bool; } public void setSystemFont(Font font) { systemFont = font; } public void setRollFont(Font font) { rollFont = font; } public void setRollLabel(String newValue) { rollLabel.setText(newValue); rollLabel.repaint(); } public void displayPrompt() { displayTextArea.append(mainDie.toString() + ":"); displayTextArea.append(getSystemPrompt()); } public void displayPrint(String strToDisp) { displayTextArea.append(strToDisp); } public void displayPrintLine(String strToDisp) { displayTextArea.append(strToDisp + "\n"); } public void displayEndline() { displayTextArea.append("\n"); } // Main entry point static public void main(String[] args) { new Polyhedra(); } }